<?php
/**
 * index.php
 * 首页加载
 * homepage 主页加载
 */

require 'common.inc.php';
$username = '';
$domain   = '';
//添加主页专用lang
include load('index.lang');
$group=PCache::read("grouporder.php");//读取缓存会员组排序
if(!$group){
	Xd_save_groupcachefile();
}

if($cityid&&$cityid!=0){
	$push_city_order='(case when city like "%|'.$cityid.'|%" or city like "%|0|%" then 1 else 2 end ),';
	$city_order='(case when areaid IN ('.$AREA[$cityid]["arrchildid"].') then 1 else 2 end ),';
}else{
	$city_order='';
}
// homepage 检测, 连接到企业黄页
if(isset($homepage) && User::checkname($homepage)) {
	$username = $homepage;
} else if(!$cityid) {
	$host = $_SERVER['HTTP_HOST'];
	if(substr($host, 0, 4) == 'www.') {
		$whost = $host;
		$host = substr($host, 4);
	} else {
		$whost = $host;
	}

	if(strpos(SITE_URL, $host) === false) {
		$www = str_replace($CFG['com_domain'], '', $host);
		if(User::checkname($www)) {
			$username = $homepage = $www;
		} else {
			if($whost == $host) {//301 xxx.com to www.xxx.com
				$w3 = 'www.'.$host;
				$c = $db->getOne("SELECT userid FROM {$db->getPre()}company WHERE domain='$w3'");
				if($c) {
					@header("HTTP/1.1 301 Moved Permanently");
					dheader('http://'.$w3);
				}
			}
			$sql =  "SELECT username,domain FROM {$db->getPre()}company WHERE domain='$whost'".($host == $whost ? '' : " OR domain='$host'");
			$c = $db->getOne($sql, 'CACHE');
			if($c) {
				$username = $homepage = $c['username'];
				$domain = $c['domain'];
			}
		}
	}
}

if($username) {
	$moduleid = 4;
	$module = 'company';
	$MOD = PCache::read('module-'.$moduleid.'.php');
	include load('company.lang');
	require DT_CORE.'/'.$module.'/common.inc.php';
	include DT_CORE.'/'.$module.'/init.inc.php';
} else {
	if($DT['safe_domain']) {
		$safe_domain = explode('|', $DT['safe_domain']);
		$pass_domain = false;
		foreach($safe_domain as $v) {
			if(strpos($DT_URL, $v) !== false) { $pass_domain = true; break; }
		}
	}

	$AREA or $AREA = PCache::read('area.php');
	require_once DT_INC.'/post.func.php';
	$area_select = ajax_area_select('areaid','选择地区',$areaid);
	$seo_title = $DT['seo_title'];
	$head_keywords = $DT['seo_keywords'];
	$head_description = $DT['seo_description'];
	$favorite =1;
	$index = 1;

	if($DT['index_html']) {
		$task_index =  intval($DT['task_index']);
		$str = $usertype.$cityid.'.';
		$html_file =  DT_ROOT.'/file/cache/index_cache/index_'.$str.$DT['file_ext'];
		if(!is_file($html_file) || $DT_TIME - @filemtime($html_file) > $task_index ) tohtml('index');
		if($usertype== 'company'){
			include template('header-company');
		}else{
			include template('header');
		}
		include($html_file);
		exit;
	}

	if($usertype== 'company'){
		$seo_title = '企业版_'.$DT['seo_title'];
		include template('index-company');
	}else{
		$isIndex = 1;
		include template('index');
	}
}